package com.xpgk.mvc.detailAnnounce;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.commons.lang.StringUtils;

import com.jfinal.aop.Before;
import com.jfinal.log.Log;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.tx.Tx;
import com.xpgk.mvc.base.BaseController;
import com.xpgk.mvc.user.UserService;
import com.xpgk.webSocket.WebSocketController;

public class DetailAnnounceController extends BaseController {
	@SuppressWarnings("unused")
	private static final Log log = Log.getLog(DetailAnnounceController.class);

	private DetailAnnounceService detailAnnounceService;
	private DetailAnnounceUserService detailAnnounceUserService;
	private UserService userService;
	public void index() {
		render("detailAnnounce.html");
	}
	@Before(Tx.class)
	public void check() throws ParseException{
		String  cusIds=getPara("ids");
		DetailAnnounce  cus=DetailAnnounce.dao.findById(cusIds);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("detailId", cusIds);
		map.put("userId", getCUserIds());
		String sql = DetailAnnounceUser.sqlId_updateDAUByUidDid;
		int count = detailAnnounceUserService.update(sql, map);
		Date date =cus.get("postDate");
		if(date!=null){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String datestr = sdf.format(date);
		cus.set("postDate", datestr);
		}
		setAttr("detailAnnounce", cus);
		render("checkAnnounce.html");
	}
	
	
	
	
	
	public  void  getDetailAnnounce(){
		List<DetailAnnounceUser>  daus =detailAnnounceUserService.getDetailAnnounce(getCUserIds());
		setAttr("daus", daus);
		render("../center/announce.html");
	}
	
	public  void  getDetailAnnounces(){
		if(splitPage!=null){
			Map<String, Object>  map  = splitPage.getQueryParam();
			map.put("userId", getCUserIds());
			splitPage.setOrderColunm("postDate");
			splitPage.setOrderMode("desc");
		}
		String json = paging(splitPage, DetailAnnounce.sqlId_splitPageSelect,DetailAnnounce.sqlId_splitPageFrom);
		renderJson(json);
	}
	
	
	
	
	
	/**
	 * 保存公告
	 * //@Before(UserValidator.class)
	  //@Before({TemporaryValidator.class,Tx.class})
	 * for (Entry<String, Object> entry : WebSocketController.connections.entrySet()) {
			System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
			String message = entry.getKey() + "您好，您有一条新的通知：test";
			WebSocketController.sendUser(entry.getKey(), message);
		}
	 */
	@Before(Tx.class)
	public void saveDetailAnnounce() {
		DetailAnnounce detailAnnounce = getModel(DetailAnnounce.class, "detailAnnounce");
		boolean flag = detailAnnounceService.save(detailAnnounce, getCUserIds(), getCUser().getStr("username"));
		String organizationIds = detailAnnounce.getStr("organizationIds");

		List<Record> users = userService.findUserByorganizationIds(organizationIds);
		List<Record> users2 = userService.findUserByorgIds(organizationIds);

		for (Record record : users2) {
			DetailAnnounceUser dau = new DetailAnnounceUser();
			if(StringUtils.isNotBlank(detailAnnounce.getStr("ids"))){
				dau.set(DetailAnnounceUser.column_detailId, detailAnnounce.getStr("ids").toString());
			}
			if(StringUtils.isNotBlank(record.getStr("ids"))){
				dau.set(DetailAnnounceUser.column_userId, record.getStr("ids").toString());
			}
			if(StringUtils.isNotBlank(detailAnnounce.getStr("title"))){
				dau.set(DetailAnnounceUser.column_title, detailAnnounce.getStr("title").toString());
			}
				dau.set(DetailAnnounceUser.column_type, "0");
			if(StringUtils.isNotBlank(record.getStr("organizationids"))){
				dau.set(DetailAnnounceUser.column_organizationIds, record.getStr("organizationids").toString());
			}
			
			
			flag=detailAnnounceUserService.save(dau);
		}

		String tilet = detailAnnounce.getStr("title");
		List<String> list2 = new ArrayList<String>();
		List<String> list3 = new ArrayList<String>();
		for (Entry<String, Object> entry : WebSocketController.connections.entrySet()) {
			list2.add(entry.getKey());
		}
		if (users != null && users.size() > 0) {
			for (Record user : users) {
				list3.add(user.getStr("username"));
			}
		}
		list2.removeAll(list3);
		if (list2 != null && list2.size() > 0) {
			for (String user : list2) {
				String message = user + "您好，您有一条新的通知：" + tilet;
				WebSocketController.sendUser(user, message+"|"+detailAnnounce.getStr("ids"));
			}
		}

		renderText(flag + "");
	}
	
}
